﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication3.权限管理
{
    public partial class editTeachers : Form
    {
        public string myEdit_ID, myEdit_name, myEdit_passwd, myEdit_pressions, myEdit_departments, myEdit_classes;

        public editTeachers()
        {
            InitializeComponent();
        }

        //填充信息
        public void fill_Info()
        {
            this.editTeachers_ID.Text = myEdit_ID;
            this.editTeachers_name.Text = myEdit_name;
            this.editTeachers_passwd.Text = myEdit_passwd;
            this.editTeachers__pressions.Text = myEdit_pressions;
            this.editTeachers_departments.Text = myEdit_departments;
            
        }
        //删除表中该条信息
        public void del_Info()
        {
            权限管理.permissionManaging.mySql.sql_search_database("delete from teacherstable  where teachersID = '"
                    + myEdit_ID + "'");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //更新信息
                string updateID, updateName, updatePasswd, updatePressions, updatedepartments;
                updateID = editTeachers_ID.Text.Trim();
                updateName = editTeachers_name.Text.Trim();
                updatePasswd = editTeachers_passwd.Text.Trim();
                updatePressions = editTeachers__pressions.Text.Trim();
                updatedepartments = editTeachers_departments.Text.Trim();
                DataSet ds = 权限管理.permissionManaging.mySql.sql_search_database("select * from teacherstable "
                        + "where teachersID= '" + updateID + "' ");             
                int rowcount = ds.Tables[0].Rows.Count;
                if (rowcount != 0) { MessageBox.Show("ID  have existed"); return; };

                if (updateID == "") { MessageBox.Show("教师ID不能为空！"); return; }
                if (updateName == "") { MessageBox.Show("教师姓名不能为空！"); return; }
                if (updatePasswd == "") { MessageBox.Show("教师密码不能为空！"); return; }
                if (updatePressions == "") { MessageBox.Show("教师权限不能为空！"); return; }
                if (updatedepartments == "") { MessageBox.Show("教师院系不能为空！"); return; }

                //更新数据库
                权限管理.permissionManaging.mySql.excute_sql("update teacherstable set "
                    + "teachersID = '" + updateID + "', "
                    + "username = '" + updateName + "', "
                    + "passwd = '" + updatePasswd + "', "
                    + "pression = '" + updatePressions + "', "
                    + "departments = '" + updatedepartments + "' "
                    + "where teachersID = '" + myEdit_ID + "' ");
                权限管理.permissionManaging.mySql.excute_sql("update sqlTableName_and_IDtable set userID = '" + updateID + "' where "
                    + "userID = '" + myEdit_ID + "'");
             
                this.Dispose();
            }
            catch
            {
                MessageBox.Show("添加过程中出现错误！");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //插入数据库
            权限管理.permissionManaging.mySql.excute_sql("update teacherstable set "
                   + "teachersID = '" + myEdit_ID + "', "
                   + "username = '" + myEdit_name + "', "
                   + "passwd = '" + myEdit_passwd + "', "
                   + "pression = '" + myEdit_pressions + "', "
                   + "departments = '" + myEdit_departments + "', "
                   + "where teachersID = '" + myEdit_ID + "' ");
            this.Dispose();
        }
    }
}
